package system

import (
	"fmt"
	"woo-server/coms/structs"
	"woo-server/coms/utils"
	"woo-server/models/db"
)

// 权限表
type ROLE struct {
	*structs.Common `gorm:"-" json:"-"`
	AdminRoleId     uint64 `gorm:"primary_key;AUTO_INCREMENT;column:admin_role_id;"` // 自增主键
	Name            string `gorm:"column:name;size:50;"`                             // 角色名称
	AdminMenuId     string `gorm:"column:admin_menu_id;"`                            // 角色菜单ID ','分隔,用find_set_int查询,性能好不好我就不管了
	Describe        string `gorm:"column:describe;size:150;"`                        // 角色描述
	db.Basics
}

func (r *ROLE) Column() []structs.ColumnCommon {

	result := make([]structs.ColumnCommon, 0)
	result = append(result, structs.ColumnCommon{Title: "ID", DataIndex: "AdminRoleId"})
	result = append(result, structs.ColumnCommon{Title: "角色名称", DataIndex: "Name"})
	result = append(result, structs.ColumnCommon{Title: "描述", DataIndex: "Describe"})
	result = append(result, structs.ColumnCommon{Title: "创建时间", DataIndex: "CreateDate"})
	result = append(result, structs.ColumnCommon{Title: "操作", DataIndex: "Operation"})
	return result
}

func (r *ROLE) Forms() []structs.FormsCommon {
	result := make([]structs.FormsCommon, 0)
	//rModel := NewROLE()
	Model := NewMenus(r.Common)
	_, list := Model.GetAllMenu(true)
	//var options OPTIONS
	options := Model.SetOption(list, 0)
	result = append(result, structs.FormsCommon{Key: "AdminRoleId", Label: "主键ID", Type: "hidden", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "AdminMenuId", Label: "权限菜单", Type: "select", Options: options})
	result = append(result, structs.FormsCommon{Key: "Name", Label: "角色名称", Type: "input", Options: []string{}})
	result = append(result, structs.FormsCommon{Key: "Describe", Label: "描述", Type: "textarea", Options: []string{}})
	return result
}

// 表名
func (r *ROLE) TableName() string {
	return "woo_admin_role"
}

// 初始化
func NewRole(com *structs.Common) *ROLE {
	d := &ROLE{}
	d.Common = com
	if com.Claim != nil {
		//d.CreateUser = com.Claim.Id
		//d.UpdateUser = com.Claim.Id
	}
	return d
}
func (r *ROLE) GetConfig() structs.CFConfig {
	list := structs.CFConfig{}
	list.Column = r.Column()
	list.Forms = r.Forms()
	return list
}

// 创建
func (r *ROLE) Create() error {
	return utils.GORM.Debug().Table(r.TableName()).Create(r).Error
}

// 批量创建
func (r *ROLE) BatchCreate(data []*ROLE) error {
	return utils.GORM.Debug().Table(r.TableName()).Create(data).Error
}

// 获取分页列表及总数
func (r *ROLE) GetListAndTotal(search string, page int, pageSize int) ([]*ROLE, int64) {
	list := make([]*ROLE, 0)
	var total int64 = 0

	// 查询
	query := utils.GORM.Debug().Table(r.TableName())

	if len(search) > 0 {
		search = fmt.Sprintf("%%%s%%", search)
		query = query.Where("name like ?", search)
	}

	// 总数
	query.Count(&total)

	// 列表
	offset := (page - 1) * pageSize
	query.Limit(pageSize).Offset(offset).Find(&list)

	return list, total
}

// 获取单挑数据
func (r *ROLE) GetFirst(where interface{}, field ...interface{}) *ROLE {
	//info := make([]*ROLE, 0)
	// 查询
	query := utils.GORM.Debug().Table(r.TableName())

	query = query.Where(where, field)

	// 总数
	//query.Count(&total)

	// 列表
	//offset := (page - 1) * pageSize
	query.First(&r)
	return r
}

func (r *ROLE) SetOption(List []*ROLE) []structs.Options {
	var AdminRoleMenus []structs.Options
	for _, v := range List {
		var option structs.Options
		option.Value = v.AdminRoleId
		option.Label = v.Name
		AdminRoleMenus = append(AdminRoleMenus, option)
	}
	return AdminRoleMenus
}

// 更新文章
func (r *ROLE) Save() error {
	// 更新
	return utils.GORM.Debug().Table(r.TableName()).Save(r).Error
}

// 删除文章
func (r *ROLE) Delete() error {
	return utils.GORM.Debug().Table(r.TableName()).Delete(r).Error
}

// 获取单条数据
func (r *ROLE) First() error {
	return utils.GORM.Debug().Table(r.TableName()).First(r).Error
}
